﻿using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using NSwag.Annotations;
using System;
using System.IO;
using System.Linq;

namespace Coldairarrow.Api.Controllers.Base_Manage
{
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("Upload")]
    public class UploadController : BaseApiController
    {
        readonly IConfiguration _configuration;
        public UploadController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        [HttpPost]
        public IActionResult UploadFileByForm()
        {
            var file = Request.Form.Files.FirstOrDefault();
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());

            string path = $"/Upload/{Guid.NewGuid().ToString("N")}/{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {
                file.CopyTo(fs);
            }

            string url = $"{_configuration["WebRootUrl"]}{path}";
            var res = new
            {
                name = file.FileName,
                status = "done",
                thumbUrl = url,
                url = url
            };

            return JsonContent(res.ToJson());
        }


        /// <summary>
        /// 视频上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadVideo()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  
                    var t = Guid.NewGuid().ToString();
                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".mp3" || _tp.ToLower() == ".mp4" || _tp.ToLower() == ".3gp" || _tp.ToLower() == ".mpeg" || _tp.ToLower() == ".mov"|| _tp.ToLower() == ".avi"|| _tp.ToLower() == ".wmv")
                        {
                            path = $"/Upload/Video/{Guid.NewGuid().ToString("N")}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadImg()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  
                    var t = Guid.NewGuid().ToString();
                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                        {
                            path = $"/Upload/Img/{Guid.NewGuid().ToString("N")}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }

        /// <summary>
        /// json文件上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadJsonFile()
        {
            var _file = Request.Form.Files;//客户端上传的文件 
            var res = new object();
            try
            {
                if (_file.Count > 0)
                {
                    string path = "";
                    long size = _file[0].Length;//文件大小                       
                    string type = _file[0].ContentType;//文件类型                       
                    string name = _file[0].FileName;//文件名                      
                    string _tp = System.IO.Path.GetExtension(name); //文件格式  
                    var t = Guid.NewGuid().ToString();
                    for (int i = 0; i < _file.Count; i++)
                    {
                        if (_tp.ToLower() == ".json")
                        {
                            path = $"/Upload/Json/{Guid.NewGuid().ToString("N")}/{_file[i].FileName}";
                            string physicPath = GetAbsolutePath($"~{path}");
                            string dir = Path.GetDirectoryName(physicPath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }

                            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
                            {
                                _file[i].CopyTo(fs);
                            }

                            string url = path;
                            res = new
                            {
                                Success = true,
                                message = _file[i].FileName,
                                data = url
                            };
                        }
                    }
                }
                else
                {
                    res = new { success = false, message = "文件不能为空！", Data = new object() };
                }
            }
            catch (Exception ex)
            {
                res = new { success = false, message = ex.Message, Data = new object() };
            }

            return JsonContent(res.ToJson());
        }
    }
}
